package top.itaso.app.ui.demos;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import top.itaso.app.R;
import top.itaso.lib.widget.helper.UniversalItemDecoration;
import top.itaso.app.databinding.FragmentDemosBinding;
import top.itaso.lib.alog.ALog;

/**
 * @Description: RecyclerView 罗列出 App 集成的各种预研示例项
 * 1.RecyclerView + addItemDecoration 实现带分割线列表
 * 2.NavController 实现页面导航（跳转）
 * @Author itsdf07
 * @Github https://github.com/itsdf07
 * @Date 2021/11/30
 */
public class DemosFragment extends Fragment {
    private static final String TAG = DemosFragment.class.getSimpleName();

    private DemosViewModel viewModel;
    private FragmentDemosBinding binding;

    private RecyclerView demosRcyView;
    private DemoItemAdapter demoAdapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ALog.vTag(TAG, "......");
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        ALog.vTag(TAG, "container: %s,savedInstanceState: %s", container, savedInstanceState);
        viewModel =
                new ViewModelProvider(this).get(DemosViewModel.class);

        binding = FragmentDemosBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        demosRcyView = binding.layoutDemos;
        demoAdapter = new DemoItemAdapter();
        demoAdapter.setOnItemClickLister(onItemClickLister);
        demosRcyView.setAdapter(demoAdapter);
        demosRcyView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        demosRcyView.addItemDecoration(itemDecoration);


        viewModel.demos.observe(getViewLifecycleOwner(), new Observer<ArrayList<DemoItemDTO>>() {
            @Override
            public void onChanged(@Nullable ArrayList<DemoItemDTO> demoResult) {
                if (demoAdapter != null && demoResult != null) {
                    demoAdapter.refreshDemoBeans(demoResult);
                }
            }
        });
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        ALog.vTag(TAG, "savedInstanceState: %s", savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onStart() {
        super.onStart();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onResume() {
        super.onResume();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onPause() {
        super.onPause();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onStop() {
        super.onStop();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ALog.vTag(TAG, "......");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        ALog.vTag(TAG, "......");
    }

    private DemoItemAdapter.IDemoItemClickLister onItemClickLister = new DemoItemAdapter.IDemoItemClickLister() {
        @Override
        public void onItemClick(View view, int position, DemoItemDTO itemDTO) {
            ALog.vTag(TAG, "正在展示第%s个 Demo ：%s", (position + 1), itemDTO.getEKey().getTitle());
//            NavController navController = NavHostFragment.findNavController(CasesFragment.this);
            NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment_content_main_drawer);
            switch (itemDTO.getEKey()) {
                case DEMO_MULTI_LIST:
                    navController.navigate(R.id.nav_multilevel_recycler);
                    break;
                case DEMO_MULTI_EXPANDABLE:
                    navController.navigate(R.id.nav_multilevel_expandable);
                    break;
                case DEMO_APPS:
                    navController.navigate(R.id.nav_apps);
                    break;
                case DEMO_PROGRESSBAR_UPLOAD:
                    navController.navigate(R.id.nav_progressbar_upload);
                    break;
                case DEMO_WIFI_SCAN:
                    navController.navigate(R.id.nav_wifi_scan);
                    break;
                case DEMO_PICKER:
                    navController.navigate(R.id.nav_picker);
                    break;
                case DEMO_FLOATING_WINDOW:
                    navController.navigate(R.id.nav_floatingwindow);
                    break;
                case DEMO_TTS:
                    navController.navigate(R.id.nav_tts);
                    break;
                case DEMO_IWB_WIDGET:
                    navController.navigate(R.id.nav_titlebar);
                    break;
                case DEMO_BTNS:
                    navController.navigate(R.id.nav_btns);
                    break;
                case DEMO_MEDIAPLAYER:
                    navController.navigate(R.id.nav_mediaplayer);
                    break;
                default:
                    break;
            }
        }
    };

    private UniversalItemDecoration itemDecoration = new UniversalItemDecoration() {
        @Override
        public Decoration getItemOffsets(int position) {
//                if (position >= viewModel.cases.getValue().size() - 1) {//控制最后一个Item是否需要有分割线
//                    return null;
//                }
            ColorDecoration decoration = new ColorDecoration();
            int dividerLine = getResources().getDimensionPixelSize(R.dimen.dimen_divider_line);
            decoration.bottom = dividerLine;
            decoration.top = dividerLine;
            decoration.left = dividerLine;
            decoration.right = dividerLine;
//            decoration.decorationColor = Color.TRANSPARENT;
            decoration.decorationColor = getResources().getColor(R.color.color_divider_line);
            return decoration;
        }
    };
}